import java.util.Stack;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 05 - 26
 * Time: 22:09
 */
class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            char ch1 = s.charAt(i);
            if((ch1 == '(') || (ch1 == '{') || (ch1 == '[')) {
                stack.push(ch1);
            }else {
                if(stack.isEmpty()) {
                    return false;
                }
                char ch2 = stack.peek();
                if((ch2 == '(' && ch1 == ')') || (ch2 == '{' && ch1 == '}')
                        || (ch2 == '[' && ch1 == ']')) {
                    return false;
                }else {
                    char ch3 = stack.pop();
                }
            }
        }
        if(!stack.isEmpty()) {
            return false;
        }
        return true;
    }
}
